<?php

class Account_RegisterController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();
        //$this->_helper->getHelper('Layout')->setLayout('default');
    }
    
    public function indexAction()
    {
         //$this->_forward('someaction', 'somecontroller');
    }

    public function showFormAction()
    {
        $show = new Ext_Register_Show();
        $this->view->formRegister = $show->show()->buildRegisterForm();
        
        $this->view->referer = Ext_Utility_Misc::referfer();
        
        $config = Ext_Config::getInstance()->getConfig("oauth");
        $adapters = $config->adapters;
        $adapters = array();
        foreach($config->adapters as $key=>$val){
            $adapters[] = $val;
        }
        $this->view->adapters = $adapters;
    }
    
    public function savePostAction()
    {
        $viewResult = new Ext_View_Result();
        
        $username        = $this->filter->filter($this->getRequest()->getPost('username'));
        $email           = $this->filter->filter($this->getRequest()->getPost('email'));
        $password        = $this->filter->filter($this->getRequest()->getPost('password'));
        $confirmPassword = $this->filter->filter($this->getRequest()->getPost('confirmPassword'));
        $nickname        = $this->filter->filter($this->getRequest()->getPost('nickname'));
        $captcha         = $this->getRequest()->getPost('captcha');
        
        $data = new Ext_Register_Save_Data();
        $data->setUsername($username);
        $data->setEmail($email);
        $data->setPassword($password);
        $data->setConfirmPassword($confirmPassword);
        $data->setNickname($nickname);
        $data->setCaptcha($captcha);

        $save = new Ext_Register_Save($data);
        $saveResult = $save->save();
        if(!$saveResult->isValid()){
            $viewResult->addMessages($saveResult->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        $userId  = $saveResult->getData();

        $oauth = $this->filter->filter($this->getRequest()->getPost('oauth'));
        if($oauth){
            $oauth = new Ext_Oauth_Login();
            $oauth->setBind($userId);
        }
        
        Ext_Auth_Front::getInstance()->auth($userId);

        $viewResult->addMessage($this->translater->_('Register and login success.'));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
}

